﻿using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.Word;
using System;

namespace drawing_print.util
{
    public class Office
    {
        /// <summary>
        /// WORD文件打印
        /// </summary>
        /// <param name="sourcePath">文件路径</param>
        public static bool wordToPdf(string sourcePath, string outPath)
        {
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Document document = null;
            try
            {
                application.Visible = false;
                application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                document = application.Documents.Open(sourcePath);
                document.ExportAsFixedFormat(outPath, WdExportFormat.wdExportFormatPDF);
                //document.PrintOut();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                document.Close();
                application.Quit();
            }
        }
        public static void wordPrint(string sourcePath)
        {
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Document document = null;
            try
            {
                application.Visible = false;
                application.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                document = application.Documents.Open(sourcePath);
                document.PrintOut();
            }
            finally
            {
                document.Close();
                application.Quit();
            }
        }
        public static bool excelToPdf(string sourcePath, string outPath)
        {
            Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
            Workbook workbook = null;
            try
            {
                application.Visible = false;
                application.DisplayAlerts = false;
                workbook = application.Workbooks.Open(sourcePath);
                //workbook.PrintOut();
                workbook.ExportAsFixedFormat(
                    XlFixedFormatType.xlTypePDF, 
                    outPath, 
                    XlFixedFormatQuality.xlQualityStandard, 
                    true, 
                    false
                    );
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                workbook.Close();
                application.Quit();
            }
        }
        public static void excelPrint(string sourcePath)
        {
            Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
            Workbook workbook = null;
            try
            {
                application.Visible = false;
                application.DisplayAlerts = false;
                workbook = application.Workbooks.Open(sourcePath);
                workbook.PrintOut();
            }
            finally
            {
                workbook.Close();
                application.Quit();
            }
        }
        public static void powerPointPrint(string sourcePath)
        {
            Microsoft.Office.Interop.PowerPoint.Application application = new Microsoft.Office.Interop.PowerPoint.Application();
            Presentation presentation = null;
            try
            {
                presentation = application.Presentations.Open(
                    sourcePath,
                    Microsoft.Office.Core.MsoTriState.msoTrue,
                    Microsoft.Office.Core.MsoTriState.msoFalse,
                    Microsoft.Office.Core.MsoTriState.msoFalse
               );
                presentation.PrintOut();
            }
            finally
            {
                presentation.Close();
                application.Quit();
            }
        }

        public static bool powerPointToPdf(string sourcePath,string outPath)
        {
            Microsoft.Office.Interop.PowerPoint.Application application = new Microsoft.Office.Interop.PowerPoint.Application();
            Presentation presentation= null;
            try
            {
                presentation = application.Presentations.Open(
                    sourcePath,
                    Microsoft.Office.Core.MsoTriState.msoTrue,
                    Microsoft.Office.Core.MsoTriState.msoFalse,
                    Microsoft.Office.Core.MsoTriState.msoFalse
               );
                presentation.ExportAsFixedFormat(
                    outPath,
                    PpFixedFormatType.ppFixedFormatTypePDF
                    );
                //presentation.PrintOut();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                presentation.Close();
                application.Quit();
            }
        }

        public static void officePrint(string sourcePath, string extension)
        {
            if (".doc".Equals(extension) || ".docx".Equals(extension))
            {
                Office.wordPrint(sourcePath);
            }
            else if (".xls".Equals(extension) || ".xlsx".Equals(extension))
            {
                Office.excelPrint(sourcePath);
            }
            else if (".ppt".Equals(extension) || ".pptx".Equals(extension))
            {
                Office.powerPointPrint(sourcePath);
            }
        }

        public static bool officeToPdf(string sourcePath, string outPath, string extension)
        {
            if (".doc".Equals(extension) || ".docx".Equals(extension))
            {
                return Office.wordToPdf(sourcePath, outPath);
            }
            else if (".xls".Equals(extension) || ".xlsx".Equals(extension))
            {
                return Office.excelToPdf(sourcePath, outPath);
            }
            else if(".ppt".Equals(extension) || ".pptx".Equals(extension))
            {
                return Office.powerPointToPdf(sourcePath, outPath);
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 判断文件类型是否支持
        /// </summary>
        /// <param name="extension"></param>
        /// <returns></returns>
        public static bool isSupport(string extension)
        {
            switch (extension)
            {
                // WORD类型
                case ".doc":
                    return true;
                case ".docx":
                    return true;
                // EXCLE类型
                case ".xls":
                    return true;
                case ".xlsx":
                    return true;
                // PPT类型
                case ".ppt":
                    return true;
                case ".pptx":
                    return true;
                default :
                    return false;
            }
        }
    }
}
